﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using DVSProject.Lib;
using DVSProject.Models;

namespace DVSProject.Controllers
{
    public class TitleServiceSummaryController : Controller
    {
        //
        // GET: /TitleServiceSummary/

        public ActionResult Index()
        {
            if (Session["User"] == null) { return RedirectToAction("LogOn", "Account"); }
            TitleServiceSummaryRepo _TitleServiceSummaryRepo = new TitleServiceSummaryRepo();
            IEnumerable<TitleServiceSummaryModels> TitleServiceSummarys = _TitleServiceSummaryRepo.GetTitleServiceSummary();
            return View(TitleServiceSummarys);
        }

        //
        // GET: /TitleServiceSummary/Details/5

        public ActionResult Details(int id)
        {
            if (Session["User"] == null) { return RedirectToAction("LogOn", "Account"); }
            TitleServiceSummaryRepo _TitleServiceSummaryRepo = new TitleServiceSummaryRepo();
            TitleServiceSummaryModels _TitleServiceSummaryModels = new TitleServiceSummaryModels();
            _TitleServiceSummaryModels = _TitleServiceSummaryRepo.GetTitleMwwDispenserById(id);

            return View(_TitleServiceSummaryModels);
        }

        //
        // GET: /TitleServiceSummary/Create

        public ActionResult Create()
        {
            if (Session["User"] == null) { return RedirectToAction("LogOn", "Account"); }
            return View();
        } 

        //
        // POST: /TitleServiceSummary/Create

        [HttpPost]
        public ActionResult Create(FormCollection collection)
        {
            if (Session["User"] == null) { return RedirectToAction("LogOn", "Account"); }
            try
            {
                // TODO: Add insert logic here
                TitleServiceSummaryRepo _TitleServiceSummaryRepo = new TitleServiceSummaryRepo();
                TitleServiceSummaryModels _TitleServiceSummaryModels = new TitleServiceSummaryModels();
                _TitleServiceSummaryModels.TitleServiceSummaryName = collection["TitleServiceSummaryName"];
                _TitleServiceSummaryModels.Std =  (!String.IsNullOrEmpty(collection["Std"])) ? Convert.ToInt32(collection["Std"]) : 0;

                bool result = _TitleServiceSummaryRepo.Create(_TitleServiceSummaryModels);
                
                return RedirectToAction("Index");
            }
            catch
            {
                return View();
            }
        }
        
        //
        // GET: /TitleServiceSummary/Edit/5
 
        public ActionResult Edit(int id)
        {
            if (Session["User"] == null) { return RedirectToAction("LogOn", "Account"); }
            TitleServiceSummaryRepo _TitleServiceSummaryRepo = new TitleServiceSummaryRepo();
            TitleServiceSummaryModels _TitleServiceSummaryModels = new TitleServiceSummaryModels();

            _TitleServiceSummaryModels = _TitleServiceSummaryRepo.GetTitleMwwDispenserById(id);

            return View(_TitleServiceSummaryModels);
        }

        //
        // POST: /TitleServiceSummary/Edit/5

        [HttpPost]
        public ActionResult Edit(int id, FormCollection collection)
        {
            if (Session["User"] == null) { return RedirectToAction("LogOn", "Account"); }
            try
            {
                // TODO: Add update logic here
                TitleServiceSummaryRepo _TitleServiceSummaryRepo = new TitleServiceSummaryRepo();
                TitleServiceSummaryModels _TitleServiceSummaryModels = new TitleServiceSummaryModels();
                _TitleServiceSummaryModels.TitleServiceSummaryId = id;
                _TitleServiceSummaryModels.TitleServiceSummaryName = collection["TitleServiceSummaryName"];
                _TitleServiceSummaryModels.Std = (!String.IsNullOrEmpty(collection["Std"])) ? Convert.ToInt32(collection["Std"]) : 0;

                bool result = _TitleServiceSummaryRepo.Edit(_TitleServiceSummaryModels);
                return RedirectToAction("Index");
            }
            catch
            {
                return View();
            }
        }

        //
        // GET: /TitleServiceSummary/Delete/5
 
        public ActionResult Delete(int id)
        {
            if (Session["User"] == null) { return RedirectToAction("LogOn", "Account"); }
            TitleServiceSummaryRepo _TitleServiceSummaryRepo = new TitleServiceSummaryRepo();
            TitleServiceSummaryModels _TitleServiceSummaryModels = new TitleServiceSummaryModels();
            _TitleServiceSummaryModels = _TitleServiceSummaryRepo.GetTitleMwwDispenserById(id);

            return View(_TitleServiceSummaryModels);
        }

        //
        // POST: /TitleServiceSummary/Delete/5

        [HttpPost]
        public ActionResult Delete(int id, FormCollection collection)
        {
            if (Session["User"] == null) { return RedirectToAction("LogOn", "Account"); }
            try
            {
                // TODO: Add delete logic here
                TitleServiceSummaryRepo _TitleServiceSummaryRepo = new TitleServiceSummaryRepo();
                TitleServiceSummaryModels _TitleServiceSummaryModels = new TitleServiceSummaryModels();
                _TitleServiceSummaryModels.TitleServiceSummaryId = id;

                bool result = _TitleServiceSummaryRepo.Delete(_TitleServiceSummaryModels);
 
                return RedirectToAction("Index");
            }
            catch
            {
                return View();
            }
        }
    }
}
